package com.ljkj.cordial.chat.fragment;


import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.adapter.FileMAdapter;
import com.ljkj.cordial.chat.base.BaseFragment;
import com.ljkj.cordial.chat.dao.AlbumTableBean;
import com.ljkj.cordial.chat.util.FileProvider;
import com.ljkj.cordial.chat.util.ObserverThread;
import com.nic.view.widget.RecycleViewDivide;
import com.yanzhenjie.album.AlbumFile;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class FilePictureFragment extends BaseFragment implements FileMAdapter.OnFileItemClickListener {


    @BindView(R.id.recycle_view)
    RecyclerView recycleView;
    Unbinder unbinder;
    private FileMAdapter fileMAdapter;


    @Override
    public int getContentLayoutId() {
        return R.layout.fragment_file_picture;
    }

    @Override
    public void initView(View rootView) {
        unbinder = ButterKnife.bind(this, rootView);
        recycleView.setLayoutManager(new LinearLayoutManager(activity));
        recycleView.setItemAnimator(new DefaultItemAnimator());
        recycleView.addItemDecoration(new RecycleViewDivide(activity, LinearLayoutManager.VERTICAL));
        fileMAdapter = new FileMAdapter();
        fileMAdapter.setOnItemClickListener(this);
        recycleView.setAdapter(fileMAdapter);
        query();
    }

    public void query() {
        ObserverThread.create().onNextThread0(new ObserverThread.OnActionListener<List<AlbumFile>>() {
            @Override
            public void onAction(List<AlbumFile> obj) {
                fileMAdapter.setList(obj);
            }

            @Override
            public List<AlbumFile> onThreadAction() {
                FileProvider fileProvider = new FileProvider(activity);
                return fileProvider.getList("%.jpeg", "%.jpg", "%.png");
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private FileMAdapter.OnFileItemClickListener listener;

    public void setListener(FileMAdapter.OnFileItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onItem(FileMAdapter adapter, View v, AlbumFile item, int position, boolean isChecked) {
        if (listener != null) {
            listener.onItem(adapter, v, item, position, isChecked);
        }
    }
}
